package com.jiesen.base.modules.certificate.textbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.baseDao.BaseServiceImpl;
import com.jiesen.base.modules.certificate.textbook.mapper.JsVideoMapper;
import com.jiesen.base.modules.certificate.textbook.model.JsVideo;
import com.jiesen.base.modules.certificate.textbook.service.JsVideoService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
* <p>
*  服务实现类
* </p>
* @author
* @since 2020-08-27
*/
@Service
public class JsVideoServiceImpl extends BaseServiceImpl<JsVideoMapper,JsVideo> implements JsVideoService {

    //添加一个视频，并根据章节信息排序
    @Override
    public boolean save(JsVideo jsVideo) {
        QueryWrapper<JsVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("textbook_id",jsVideo.getTextbookId())
                    .orderByDesc("sort");
        List<JsVideo> jsVideos = baseMapper.selectList(queryWrapper);
        if(jsVideos.size()==0){
            jsVideo.setSort(1);
        }else{
            jsVideo.setSort(jsVideos.get(0).getSort()+1);
        }
        return super.save(jsVideo);
    }


    @Override
    public List<JsVideo> findByBookIds(List<Integer> ids, Integer detailds) {
        QueryWrapper<JsVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("textbook_id",ids)
                    .eq("detailed",detailds);
        List<JsVideo> jsVideos = baseMapper.selectList(queryWrapper);
        return jsVideos;
    }

    @Override
    public List<JsVideo> findByBookIds(Integer id, Integer detailds) {
        QueryWrapper<JsVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("textbook_id",id)
                    .eq("detailed",detailds);
        List<JsVideo> jsVideos = baseMapper.selectList(queryWrapper);
        return jsVideos;
    }

    @Override
    public int deleteByBookId(Serializable textbookId) {
        QueryWrapper<JsVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("textbook_id", textbookId);
        return baseMapper.delete(queryWrapper);
    }

}
